/**
 * Created by Administrator on 2017/11/20.
 */
'use strict';
var fs = require('fs');
var formidable = require('formidable');
var common = require('../models/common.js');
var BookModel = require('../models/Book.js');//书本Schema

exports.rankKey=function(req,res){
    let session=false;
    if(req.session.username){
        session=true;
    }
    if(req.params.key=="male"){
        res.render('rank',{
            page:'male',
            session:session
        });
    }else if(req.params.key=="female"){
        res.render('rank',{
            page:'female',
            session:session
        });
    }else{
        res.render('error',{page:'error'});
    }
};
exports.rankMaleKey=function(req,res){
    let session=false;
    if(req.session.username){
        session=true;
    }
    let key=req.params.key;
    if(key=='yuepiao'){
        res.render('./rank/content.ejs',{
            page:'yuepiao',
            typeName:"原创风云榜",
            session:session
        });
    }else if(key=='hotsales'){
        res.render('./rank/content.ejs',{
            page:'hotsales',
            typeName:"畅销榜",
            session:session
        });
    }else if(key=='click'){
        res.render('./rank/content.ejs',{
            page:'click',
            typeName:"点击榜",
            session:session
        });
    }else if(key=='rec'){
        res.render('./rank/content.ejs',{
            page:'rec',
            typeName:"推荐榜",
            session:session
        });
    }else if(key=='reward'){
        res.render('./rank/content.ejs',{
            page:'reward',
            typeName:"打赏榜",
            session:session
        });
    }else if(key=='update'){
        res.render('./rank/content.ejs',{
            page:'update',
            typeName:"更新榜",
            session:session
        });
    }else if(key=='sign'){
        res.render('./rank/content.ejs',{
            page:'sign',
            typeName:"签约榜",
            session:session
        });
    }else{
        res.render('error',{page:'error'});
    }
};
exports.getProminent=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let type=fields.type;
        switch(type){
            case "fyb":
                BookModel.findFnLimit({},{month:-1},5,function(err,docs){
                    if(err){
                        res.json({code:0,msg:"失败",info:[]});
                        return;
                    }
                    res.json({code:1,msg:"成功",info:docs});
                });
                break;
            case "cxb":
                BookModel.findFnLimit({},{subscribe:-1},5,function(err,docs){
                    if(err){
                        res.json({code:0,msg:"失败",info:[]});
                        return;
                    }
                    res.json({code:1,msg:"成功",info:docs});
                });
                break;
            case "djb":
                BookModel.findFnLimit({},{clickCnt:-1},5,function(err,docs){
                    if(err){
                        res.json({code:0,msg:"失败",info:[]});
                        return;
                    }
                    res.json({code:1,msg:"成功",info:docs});
                });
                break;
            case "tjb":
                BookModel.findFnLimit({},{recomm:-1},5,function(err,docs){
                    if(err){
                        res.json({code:0,msg:"失败",info:[]});
                        return;
                    }
                    res.json({code:1,msg:"成功",info:docs});
                });
                break;
            case "dsb":
                BookModel.findFnLimit({},{reward:-1},5,function(err,docs){
                    if(err){
                        res.json({code:0,msg:"失败",info:[]});
                        return;
                    }
                    res.json({code:1,msg:"成功",info:docs});
                });
                break;
            case "gxb":
                BookModel.findFnLimit({},{updateTime:-1},5,function(err,docs){
                    if(err){
                        res.json({code:0,msg:"失败",info:[]});
                        return;
                    }
                    res.json({code:1,msg:"成功",info:docs});
                });
                break;
            case "qyb":
                BookModel.findFnLimit({isVip:1},{updateTime:-1},5,function(err,docs){
                    if(err){
                        res.json({code:0,msg:"失败",info:[]});
                        return;
                    }
                    res.json({code:1,msg:"成功",info:docs});
                });
                break;
        }
    })
};
exports.getRankBookCount=function(req,res){
    BookModel.findConut({},function(err,count){
        if(err){
            res.json({code:0,msg:"失败",count:0});
            return;
        }
        res.json({code:1,msg:"成功",count:count});
    })
};
exports.getRankBook=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let type=fields.type;
        let tag=fields.tag;
        let page=parseInt(fields.page);
        let pagenum=page*10;
        let obj={};
        if(tag==-1){

        }else{
            obj={catId:tag};
        }
        function getBook(obj,type,data){
            let filter=`{"${type}":-1}`;
            filter=JSON.parse(filter);
            if(data){
                if(tag==-1){
                    obj={isVip:1}
                }else{
                    obj={catId:tag,isVip:1};
                }
            }
            var p1=new Promise(function(resolve,reject){
                BookModel.findConut(obj,function(err,count){
                    if(err){
                        reject(err);
                        return;
                    }
                    resolve(count);
                })
            });
            p1.then(function(count){
                BookModel.findLimitSkip(obj,10,pagenum,filter,function(err,docs){
                    if(err){
                        res.json({code:0,msg:"失败",info:[],count:count});
                    }
                    res.json({code:1,msg:"成功",info:docs,count:count});
                });
            },function(err){
                console.log(err);
                res.json({code:0,msg:"失败",info:[],count:0});
            });
        }
        switch(type){
            case "yuepiao":
                getBook(obj,"month");
                break;
            case "hotsales":
                getBook(obj,"subscribe");
                break;
            case "click":
                getBook(obj,"clickCnt");
                break;
            case "rec":
                getBook(obj,"recomm");
                break;
            case "reward":
                getBook(obj,"reward");
                break;
            case "update":
                getBook(obj,"updateTime");
                break;
            case "sign":
                getBook(obj,"updateTime",true);
                break;
            default:
                res.json({code:1,msg:"成功",info:[],count:0});
        }
    });
};